# Run CFAs ----------------------------------------------------------------
setwd("./Code/")
source("./appendix/appendix4_study3_cfas.R")
source("./appendix/appendix4_study4_cfas.R")


# Need for Chaos ----------------------------------------------------------
# Study 3
v_f <- c("nfc_teardown",
             "nfc_needchaos",
             "nfc_destroy",
             "nfc_disastFun",
             "nfc_disastRebuild",
             "nfc_burnsociety",
             "nfc_burninstits")
v_r <- c("nfc_clearrules",
             "nfc_upholdorder",
             "nfc_chaosupset",
             "nfc_respectproduct",
             "nfc_workinside",
             "nfc_protectinstits",
             "nfc_greatthings")
omega(mt[, v_f], plot = F)
omega(mt[, v_r], plot = F)

pars <- parameterEstimates(out_s3_method)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:7])
mean(loadings$est[8:14])

# Study 4
v_f <- c("nfc_teardown",
             "nfc_needchaos",
             "nfc_destroy",
             "nfc_disastFun",
             "nfc_disastRebuild",
             "nfc_burnsociety",
             "nfc_burninstits")
v_r <- c("nfc_clearrules",
             "nfc_upholdorder",
             "nfc_chaosupset",
             "nfc_respectproduct",
             "nfc_workinside",
             "nfc_protectinstits",
             "nfc_greatthings"
             )
omega(bov[, v_f], plot = F)
omega(bov[, v_r], plot = F)

pars <- parameterEstimates(out_s4_method)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:7])
mean(loadings$est[8:14])
# Populism ----------------------------------------------------------------
# Study 3
v_f <- c("pop_fewints",
         "pop_crooked",
         "pop_nomethink")
v_r <- c("pop_polsimprove",
         "pop_yesmethink",
         "pop_benefitall")
omega(mt[, v_f], plot = F)
omega(mt[, v_r], plot = F)

pars <- parameterEstimates(out_s3_pop_method)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:3])
mean(loadings$est[4:6])

# Study 4
v_f <- c("pop_fewints",
         "pop_crooked",
         "pop_nomethink")
v_r <- c("pop_polsimprove",
         "pop_yesmethink",
         "pop_benefitall")
omega(bov[, v_f], plot = F)
omega(bov[, v_r], plot = F)
pars <- parameterEstimates(out_s4_pop_method)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:3])
mean(loadings$est[4:6])

# Political Violence ------------------------------------------------------
# Study 3
v_f <- c("viol_threatpols",
         "viol_bricks",
         "viol_stopbadgovt",
         "viol_bullets")
v_r <- c("viol_noviol",
         "viol_nonviolprot",
         "viol_violunaccept",
         "viol_notit4tat")
omega(mt[, v_f], plot = F, flip = F)
omega(mt[, v_r], plot = F)

pars <- parameterEstimates(out_s3_viol_method)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:4])
mean(loadings$est[5:8])


# Study 4
v_f <- c("viol_threatpols",
         "viol_bricks",
         "viol_stopbadgovt",
         "viol_bullets")
v_r <- c("viol_noviol",
         "viol_nonviolprot",
         "viol_violunaccept",
         "viol_notit4tat")
omega(bov[, v_f], plot = F)
omega(bov[, v_r], plot = F)


pars <- parameterEstimates(out_s4_viol_method)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:4])
mean(loadings$est[5:8])

# Conspiratorial Thinking -------------------------------------------------
# Study 3
v_f <- c("consp_plots",
         "consp_fewppl",
         "consp_dkrun",
         "consp_wars")
v_r <- c("consp_schoolexps",
         "consp_complex",
         "consp_democ",
         "consp_US")
omega(mt[, v_f], plot = F)
omega(mt[, v_r], plot = F)


pars <- parameterEstimates(out_s3_consp_method)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:4])
mean(loadings$est[5:8])


# Study 4
v_f <- c("consp_plots",
         "consp_fewppl",
         "consp_dkrun",
         "consp_wars")
v_r <- c("consp_schoolexps",
         "consp_democWill",
         "consp_democ",
         "consp_US")
omega(bov[, v_f], plot = F)
omega(bov[, v_r], plot = F)

pars <- parameterEstimates(out_s4_consp_method)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:4])
mean(loadings$est[5:8])



# Racial Resentment -------------------------------------------------------
# Study 3
v_f <- c("rr_specfavr",
         "rr_thard")
v_r <- c("rr_pdisc",
         "rr_dless")
omega(mt[, v_f], plot = F, nfactors = 1)
omega(mt[, v_r], plot = F, nfactors = 1)

pars <- parameterEstimates(out_s3_rr)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:2])
mean(loadings$est[2:4])


# Study 4
v_f <- c("rr_specfavr",
         "rr_thard")
v_r <- c("rr_pdisc",
         "rr_dless")
omega(bov[, v_f], plot = F, nfactors = 1)
omega(bov[, v_r], plot = F, nfactors = 1)

pars <- parameterEstimates(out_s4_rr)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:2])
mean(loadings$est[2:4])

# Hostile Sexism ----------------------------------------------------------
# Study 3
v_f <- c("hs_control",
         "hs_exaggerate",
         "hs_leash")
v_r <- c("hs_reasonable",
         "hs_feministpower",
         "hs_fewwomen")
omega(mt[, v_f], plot = F)
omega(mt[, v_r], plot = F)

pars <- parameterEstimates(out_s3_hs_method)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:3])
mean(loadings$est[4:6])


# Study 4
v_f <- c("hs_control",
         "hs_exaggerate",
         "hs_leash")
v_r <- c("hs_reasonable",
         "hs_feministpower",
         "hs_fewwomen")

omega(bov[, v_f], plot = F)
omega(bov[, v_r], plot = F)

pars <- parameterEstimates(out_s4_hs_method)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:3])
mean(loadings$est[4:6])

# AntiDemocratic Attitudes ------------------------------------------------
# Study 4
v_f <- c("antidemoc_handout",
         "antidemoc_force",
         "antidemoc_patriotic",
         "antidemoc_bendrules")
v_r <- c("antidemoc_debate",
         "antidemoc_acceptelex",
         "antidemoc_roomtradits",
         "antidemoc_leaderlaws")

omega(bov[, v_f], plot = F)
omega(bov[, v_r], plot = F)

pars <- parameterEstimates(out_s4_antidem_method)
loadings <- pars[which(pars$op == "=~" & pars$lhs != "method"), ]
mean(loadings$est[1:4])
mean(loadings$est[5:8])
